package top.kikt.book.core.handlers

import okhttp3.HttpUrl.Companion.toHttpUrl
import org.jsoup.nodes.Document
import top.kikt.book.core.entity.ChapterMetaEntity
import top.kikt.book.core.handlers.common.BaseBookHandler
import top.kikt.book.core.handlers.common.CommonBookHandler
import java.nio.charset.Charset

class Ygshu : BaseBookHandler(), CommonBookHandler {

    override fun forceCharset(): Charset? = null

    override val host: String
        get() = "www.ygshu.com"

    override fun watermark() =
        mapOf(
            Regex("。.+ＣＯＭ") to "。"
        )

    override fun parseChapterMeta(document: Document, menuUrl: String): List<ChapterMetaEntity> {
        val nodes = document.select("body > div.listmain > dl > dd > a")

        return nodes.mapIndexed { index, it ->
            val a = it.select("a")[0]
            val href = a.attr("href")
            val url = menuUrl.toHttpUrl().resolve(href)
            val name = a.text().trim()
            ChapterMetaEntity(name, index, url.toString(), it)
        }.toList()
    }

}